package com.mdnote.jianzhi_offer.stack;

import java.util.Stack;

/**
 * @author Rhythm-2019
 * @version 1.0
 * @date 2020/10/12
 * @description 剑指 Offer 09. 用两个栈实现队列
 */
public class Offer09I {


    public static void main(String[] args) {
        CQueue cQueue = new CQueue();
        cQueue.appendTail(1);
        cQueue.appendTail(2);
        cQueue.appendTail(3);

        System.out.println(cQueue.deleteHead());
        System.out.println(cQueue.deleteHead());
        System.out.println(cQueue.deleteHead());
    }

    private static class CQueue {

        // 用于插入
        private Stack<Integer> stack1;

        // 用于删除
        private Stack<Integer> stack2;

        public CQueue() {
            this.stack1 = new Stack<>();
            this.stack2 = new Stack<>();
        }

        public void appendTail(int value) {
            stack1.push(value);
        }

        public int deleteHead() {
            if (stack2.isEmpty()) {
                while (!stack1.isEmpty()) {
                    stack2.push(stack1.pop());
                }
            }
            return stack2.isEmpty() ? -1 : stack2.pop();
        }
    }
}
